package com.lyf.controller.admin;

import com.lyf.result.Result;
import com.lyf.service.admin.ReportService;
import com.lyf.vo.OrderReportVO;
import com.lyf.vo.SalesTop10ReportVO;
import com.lyf.vo.TurnoverReportVO;
import com.lyf.vo.UserReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;

@Api(tags = "数据统计")
@Slf4j
@RequestMapping("/admin/report")
@RestController
public class ReportController {
    @Autowired
    private ReportService reportService;

    @ApiOperation("营业额统计")
    @RequestMapping("/turnoverStatistics")
    public Result turnoverStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                     @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("营业额统计：{}，{}",begin,end);
        TurnoverReportVO turnoverReportVO = reportService.turnoverStatistics(begin, end);
        return Result.success(turnoverReportVO);
    }
    @ApiOperation("用户统计")
    @RequestMapping("/userStatistics")
    public Result userStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                 @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("用户统计：{}，{}",begin,end);
        UserReportVO userReportVO =  reportService.userStatistics(begin, end);
        return Result.success(userReportVO);
    }

    @ApiOperation("订单统计")
    @RequestMapping("/ordersStatistics")
    public Result ordersStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                   @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("订单统计：{}，{}",begin,end);
        OrderReportVO orderReportVO =  reportService.ordersStatistics(begin, end);
        return Result.success(orderReportVO);
    }

    @ApiOperation("销量统计")
    @RequestMapping("/top10")
    public Result top10(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("销量统计：{}，{}",begin,end);
        SalesTop10ReportVO salesTop10ReportVO = reportService.top10(begin, end);
        return Result.success(salesTop10ReportVO);
    }

    @ApiOperation("导出数据Excel报表")
    @RequestMapping("/export")
    public Result export(HttpServletResponse response) throws IOException {
        log.info("导出数据Excel报表");
        reportService.exportBusinessesData(response);
        return Result.success();
    }
}
